Page({
  data: {
    totalMoney: 0,
    qxChecked:false,
    quantity: {
      count: 1,
      min: 1,
      max: 20,
      delStatus: 'disabled',
      addStatus: 'normal'
    },
    pointCarts: [],
    // 商品详情介绍
    carts: [],
  },
  delCount: function(e) {
    var index = e.target.dataset.index;
    var count = this.data.carts[index].Quantity;
    var skuid = this.data.carts[index].SkuId;
    var that = this;
    console.log(skuid);
    if (count > 1) {
      wx.request({
        url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          "action": "UpdateCartPayNum",
          'userid': wx.getStorageSync('userid'),
          "skuId": skuid,
          "isjia": 0
        },
        dataType: "json",
        success: function (res) {
          if (res.data.success) {
            // 商品总数量-1
            if (count > 1) {
              that.data.carts[index].Quantity--;
            }
            console.log(that.data.carts[index]);
            // 将数值与状态写回  
            that.setData({
              carts: that.data.carts
            });
            that.priceCount(index);
          }
        }
      })
    }
 

  },
  /* 加数 */
  addCount: function(e) {
    var index = e.target.dataset.index;
    var count = this.data.carts[index].Quantity;
    var skuid = this.data.carts[index].SkuId;
    var that=this;
    console.log(skuid);
    if(count<10){
      wx.request({
        url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          "action": "UpdateCartPayNum",
          'userid': wx.getStorageSync('userid'),
          "skuId": skuid,
          "isjia": 1
        },
        dataType: "json",
        success: function (res) {
          console.log(res.data.success);
          if (res.data.success) {
            // 商品总数量-1
            that.data.carts[index].Quantity++;
            console.log(that.data.carts[index]);
            // 将数值与状态写回  
            that.setData({
              carts: that.data.carts
            });
            that.priceCount(index);
          }
        }
      })
    }
 
  },
  priceCount: function(e) {
    this.data.carts[e].SubTotal = this.data.carts[e].MemberPrice * this.data.carts[e].Quantity;
    this.setData({
      carts: this.data.carts
    })
    var Total = 0;
    for (var i = 0; i < this.data.carts.length; i++) {
      Total += parseInt(this.data.carts[i].SubTotal);
    }
    this.setData({
      totalMoney: Total
    })
  },
  // 去结算
  toBuy() {
    if (this.data.carts.length + this.data.pointCarts.length > 0) {
      wx.navigateTo({
        url: '../order/order',
      })
    }

  },
  removeCart: function(e) {
    var skuId = e.currentTarget.dataset.skuid;
    var carttype = e.currentTarget.dataset.type;
    var that = this;

    wx.showModal({
      title: '提示',
      content: '是否移除购物车',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              'action': 'ProcessDeleteCartProduct',
              'userid': wx.getStorageSync('userid'),
              'skuId': skuId,
              'type': carttype
            },
            dataType: "json",
            success: function(res) {
              var data = res.data;
              if (data.Status == "OK") {
                wx.showModal({
                  title: '提示',
                  content: '移出成功',
                  showCancel: false,
                  success: function() {
                    that.onLoad();
                  }
                })
              }
            }
          })
        }
      }
    })

  },
  editprocart: function (e) {
    var skuId = e.currentTarget.dataset.skuid;
    var ischecked = e.currentTarget.dataset.ischecked;
    var that = this;
  
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': 'UpdateCartChecked',
        'userid': wx.getStorageSync('userid'),
        'skuId': skuId,
        'checked':ischecked,
        'isqx':0
      },
      dataType: "json",
      success: function (res) {
        if (res.data.success) {
          that.onLoad();
        }
      }
    })

  },
  qxprocart:function(e){
    var iscartqx = e.currentTarget.dataset.iscartqx;
    var ischecked = e.currentTarget.dataset.ischecked;
    var that = this;

    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': 'UpdateCartChecked',
        'userid': wx.getStorageSync('userid'),
        'skuId': '',
        'checked': -1,
        'isqx': iscartqx
      },
      dataType: "json",
      success: function (res) {
        if (res.data.success) {
          that.onLoad();
        }
      }
    })

  },
  onLoad: function() {
    
    wx.setNavigationBarTitle({
      title: '购物车',
    });


    var that = this;
    that.setData({
      qxChecked: false
    })
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': 'getShoppingCart',
        'userid': wx.getStorageSync('userid')
      },
      dataType: "json",
      success: function(res) {
        if (res.data != null) {
          console.log(res.data);
          if (res.data[0] != null) {
            that.setData({
              carts: res.data[0][0].LineItems,
              totalMoney: res.data[0][0].Total,
              qxChecked: res.data[0][0].isqxChecked
            })
          } else {
            that.setData({
              carts: [],
              totalMoney: 0
            })
          }
          if (res.data[1] != null) {
            that.setData({
              pointCarts: res.data[1][0].LineItems,
            })
          } else {
            that.setData({
              pointCarts: [],
            })
          }
        } else {
          that.setData({
            carts: [],
            totalMoney: 0
          })
        }
      }
    })
  },
  onReady: function() {

  },
  onShow: function() {
    this.onLoad();
  }
});